trigger Assignment6_3 on Student__c (before insert) {
    //TODO: handling in case we get more than 1000 recods here
    Map<id, Class__c> clsMap = new Map<id, Class__c>([Select MaxSize__c, Number_Of_Students__c from Class__c]);

    //since we know only one record will be here in Trigger.new
    for(Student__c student : Trigger.new) {
      System.debug(logginglevel.INFO,'***** class ref' + student.Class__c);   
      
      /***** DISCARDED *****/      
      /*List<Class__c> studentClassList = new List<Class__c>([Select MaxSize__c, Number_Of_Students__c from Class__c where Class__c.id =: student.Class__c limit 1]);
      
      for(Class__c cls : studentClassList) {
         System.debug(logginglevel.INFO,'***** cls.MaxSize__c == ' + cls.MaxSize__c + ' cls.Number_Of_Students__c == ' + cls.Number_Of_Students__c );
         if( cls.MaxSize__c <= cls.Number_Of_Students__c){
           student.addError('Can not create new student. Max limit of class has already been reached');
         } 
      }*/
      Class__c cls = clsMap.get(student.Class__c);  
      if(cls != null ) {
          if( cls.MaxSize__c <= cls.Number_Of_Students__c){
           student.addError('Can not create new student. Max limit of class has already been reached');
         } 
      }
       
    }

}